package com.daz.common.publicDict.server.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daz.common.publicDict.dao.IPublicDictDao;
import com.daz.common.publicDict.pojo.InquireMark;
import com.daz.common.publicDict.pojo.SystemParam;
import com.daz.common.publicDict.pojo.publicDictPojo;
import com.daz.common.publicDict.server.IPublicDictServer;
import com.daz.student.dao.IInquireAnswerDao;
import com.daz.student.pojo.InquireAnswerPojo;
import com.daz.teacher.menuTree.dao.iMenuTreeDao;
import com.daz.teacher.menuTree.pojo.menuTreePojo;
import com.daz.teacher.menuTree.server.imenuTreeServer;
import com.daz.teacher.teacherInfo.dao.iTeacherDao;
import com.daz.teacher.teacherInfo.pojo.teacher;
@Service
public class PublicDictServer implements IPublicDictServer{
	@Autowired
	private IPublicDictDao publicDictDao;
	@Autowired
	private iMenuTreeDao menuTreeDao;
	@Autowired
	private iTeacherDao teacherDao;
	@Autowired
	private IInquireAnswerDao inquireAnswerDao;
	public List<publicDictPojo> searchPublicDict(Map<String, Object> map)throws Exception{
		return publicDictDao.searchPublicDict(map);
	}
	public publicDictPojo searchOnePublicDict(Map<String, Object> map) throws Exception{
		return publicDictDao.searchOnePublicDict(map);
	}
	public Boolean addSystemParam(publicDictPojo pojo) throws Exception{
		if(publicDictDao.addSystemParam(pojo) == 1){
			return true;
		}else {
			return false;
		}
	}
	public Boolean updateSystemParam(publicDictPojo pojo) throws Exception{
		if(publicDictDao.updateSystemParam(pojo) == 1){
			return true;
		}else{
			return false;
		}
	}
	public Boolean deleteSystemParam(List<String> list) throws Exception{
		if(publicDictDao.deleteSystemParam(list) == list.size()){
			return true;
		}else{
			return false;
		}
	}
	
	
	public int updateStatus(Map<String, Object> map)throws Exception{
		if("1".equals(map.get("paramId"))){
			menuTreePojo menu = new menuTreePojo();
			menu.setStatus((String)map.get("valueId"));
			menu.setId("208");
			menuTreeDao.updateMenuTree(menu);
			if("0".equals(map.get("valueId"))){
				List<teacher> teacherList= teacherDao.getTeacher(null);
				List<InquireMark> inquireMarkList = new ArrayList<>();
				for(int i = 0;i<teacherList.size();i++){
					Map<String, Object> map2 = new HashMap<String, Object>();
					map2.put("teacherId",teacherList.get(i).getTeacherId());
					List<InquireAnswerPojo> inquireList = inquireAnswerDao.selectInquireAnswer(map2);
					long markLong = 0;
					int mm = inquireList.size();
					for(int j = 0;j<mm;j++){
						markLong+=Long.parseLong(inquireList.get(j).getInquireAnswer());
					}
					String mark="0";
					if(mm != 0){
						mark = markLong/mm+"";
					}
					
					InquireMark inquireMark = new InquireMark();
					inquireMark.setMark(mark);
					inquireMark.setTeacherId(teacherList.get(i).getTeacherId());
					Map<String, Object> map3 = new HashMap<>();
					map3.put("publicCode", "prom");
					inquireMark.setProm(publicDictDao.selectStatus(map3).get(0).getValue().getValueId());
					inquireMarkList.add(inquireMark);
				}
				publicDictDao.addInquareMarkMatch(inquireMarkList);
			}else{
				//inquireAnswerDao.deleteAll();
			}
		}
		return publicDictDao.updateStatus(map);
	}
	public List<SystemParam> selectStatus(Map<String, Object> map) throws Exception{
		return publicDictDao.selectStatus(map);
	}
	
	public List<InquireMark> selectMark(Map<String, Object> map)throws Exception{
		return publicDictDao.selectMark(map);
	}
}
